import { CourseSlot } from '../../types/courseSlot'
import { execute, select } from '../../utils/db'
import { Page, PageReq } from '../../types/page'

export class CourseSlotDao {
  /**
   *  id?: number
  course_id?: number
  course_time_id?: string
  ar_duration?: number
  sound_bars?: number
  secens_id?: number
   * 创建排课课程
   */
  static async create(courseSlot: Omit<CourseSlot, 'id'>): Promise<number | undefined> {
    const result = await execute(
      `INSERT INTO course_slot (
        course_id, 
        course_time_id, 
        ar_duration, 
        sound_bars, 
        secens_id
      ) VALUES (?, ?, ?, ?, ?) RETURNING id`,
      [
        courseSlot.course_id,
        courseSlot.course_time_id,
        courseSlot.ar_duration,
        courseSlot.sound_bars,
        courseSlot.secens_id
      ]
    )
    return result?.lastInsertId
  }

  /**
   * 更新排课课程
   */
  static async update(id: number, courseSlot: Partial<CourseSlot>): Promise<void> {
    const fields = []
    const values = []

    if (courseSlot.course_id !== undefined) {
      fields.push('course_id = ?')
      values.push(courseSlot.course_id)
    }
    if (courseSlot.course_time_id !== undefined) {
      fields.push('course_time_id = ?')
      values.push(courseSlot.course_time_id)
    }
    if (courseSlot.ar_duration !== undefined) {
      fields.push('ar_duration = ?')
      values.push(courseSlot.ar_duration)
    }
    if (courseSlot.sound_bars !== undefined) {
      fields.push('sound_bars = ?')
      values.push(courseSlot.sound_bars)
    }
    if (courseSlot.secens_id !== undefined) {
      fields.push('secens_id = ?')
      values.push(courseSlot.secens_id)
    }

    if (fields.length === 0) return

    await execute(
      `UPDATE course_slot SET ${fields.join(', ')} WHERE id = ?`,
      [...values, id]
    )
  }

  /**
   * 删除排课课程
   */
  static async delete(id: number): Promise<void> {
    await execute('DELETE FROM course_slot WHERE id = ?', [id])
  }

  /**
   * 获取排课课程详情
   */
  static async getById(id: number): Promise<CourseSlot> {
    const result = await select<CourseSlot>(
      'SELECT * FROM course_slot WHERE id = ? LIMIT 1',
      [id]
    )
    return result[0]
  }

  /**
   * 获取排课课程列表
   */
  static async list(): Promise<CourseSlot[]> {
    const result = await select<CourseSlot[]>('SELECT * FROM course_slot')
    return result
  }

  /**
   * 分页查询排课课程列表
   */
  static async page(params: PageReq<CourseSlot>): Promise<Page<CourseSlot>> {
    const { pageNum, pageSize } = params
    const offset = (pageNum - 1) * pageSize
    const courseSlot = params.params
    
    const fields = []
    const values = []

    if (courseSlot.course_id !== undefined) {
      fields.push('course_id = ?')
      values.push(courseSlot.course_id)
    }
    if (courseSlot.course_time_id !== undefined) {
      fields.push('course_time_id = ?')
      values.push(courseSlot.course_time_id)
    }
    if (courseSlot.ar_duration !== undefined) {
      fields.push('ar_duration = ?')
      values.push(courseSlot.ar_duration)
    }
    if (courseSlot.sound_bars !== undefined) {
      fields.push('sound_bars = ?')
      values.push(courseSlot.sound_bars)
    }
    if (courseSlot.secens_id !== undefined) {
      fields.push('secens_id = ?')
      values.push(courseSlot.secens_id)
    }
    
    const condition = fields.length > 0 ? `WHERE ${fields.join(' AND ')}` : ''
    const [totalResult, listResult] = await Promise.all([
      select('SELECT COUNT(*) as count FROM course_slot ' + condition, values),
      select(`SELECT * FROM course_slot ${condition} LIMIT ${pageSize} OFFSET ${offset}`, values)
    ])

    return {
      total: totalResult[0].count || 0,
      list: (listResult as unknown as CourseSlot[]) || []
    }
  }
}